<?php
Class Ecoupon extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->data['controller'] = 'ecoupon';
        $this->load->model('Mecoupon');
        $this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 2, $this->session->userdata('admin_id'));
        
    }
    
    public function index()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mecoupon->getEcouponDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        //echo "<pre>"; die(print_r($this->data['data']));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            if($this->_per->del != 4)
            {
                $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/ecoupon/index');
                exit();
            }
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Mecoupon->deleteEcouponDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/ecoupon/index";</script>');
            }
        }
        
        $this->data['action'] = 'listcoupon';
        $this->load->view('template/layout', $this->data);
    }
    
    
    public function addecoupon()
    {
//        echo PROJECT_PATH;die;
        if($this->_per->add != 2)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/ecoupon/index');
            exit();
        }
        
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', 'Tên Ecoupon', 'required');
        $this->form_validation->set_rules('sale_off', 'Số tiền giảm giá', 'required');
        $this->form_validation->set_rules('amount', 'Số lượng', 'required|less_than[1000]|greater_than[0]');
        $this->form_validation->set_rules('date_user', 'Thời hạn sử dụng', 'required');
        $this->form_validation->set_rules('brief', 'Giới thiệu Ecoupon', 'required');
        $this->form_validation->set_rules('subject_email', 'Subject Email', 'required');
        $this->form_validation->set_rules('note_email', 'Lưu ý Email', 'required');
        
        /*$data['type'] = $this->getParamInt('incentives_type');
        if($data['type'] == 1){
            $this->form_validation->set_rules('sl_month', 'Tháng sử dụng counpon', 'required');
            $this->form_validation->set_rules('sl_year', 'Năm sử dụng counpon', 'required');
        }*/
        $data['type_sl'] = $this->getParamInt('restaurant_target');
        if($data['type_sl'] == 1){
            $this->form_validation->set_rules('restaurants', 'Nhà hàng', 'required');
        }
        
        
        if(in_array($this->session->userdata('coupon'), array(null, '', '0')) || in_array($this->session->userdata('coupon_qr'), array(null, '', '0')) || in_array($this->session->userdata('img_qrcode'), array(null, '', '0')))
        {
            
        }
        
        if($this->form_validation->run() === TRUE)
        {
            
            /*if($data['type'] == 1){
                $data['month'] = $this->getParamInt('sl_month');
                $data['year'] = $this->getParamInt('sl_year');
            }else{
                $data['month'] = $data['year']  = 0;
            }*/
            $data['month'] = $data['year']  = 0;
            if($data['type_sl'] == 1){
                $arr_restaurant = $this->getParamArray('restaurants');
            }else{
                $arr_restaurant = 0;
            }
            
			/*
			//ảnh đại diện
            /*$dataimage = $this->getParamString('img_incens_hd_');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'incentives-'.$timenow.'.jpg';
                $path = 'images/incentives/'.date('Ym').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$dataimage);
                $this->savefile($exp,$paths,$nameimagess);
                $img_thumbnail = $path.$nameimagess;
            }else{
                $img_thumbnail = '';
            }*/
            
            $path1 = 'images/ecoupon/';
			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
			$config['upload_path'] = '../data/'.$path1;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2000';
			$config['max_width']  = '10240';
			$config['max_height']  = '7680';
			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
			$this->load->library('upload', $config);
			if( !$this->upload->do_upload('fileupload')){
				$img = '';
			}else{
				$data_img = $this->upload->data();
				$img = $path1.$data_img['file_name'];
			}

			//$img_thumbnail = ''; //fake
            
            $run = $this->Mecoupon->addEcouponDB($data, $arr_restaurant, $this->session->userdata('admin_id'), $this->session->userdata('coupon'), $this->session->userdata('coupon_qr'), $this->session->userdata('img_qrcode'), $this->data['baseurl'],$img);
            
            //$run = $this->Mecoupon->addEcouponDB($data, $arr_restaurant, $this->session->userdata('admin_id'), $this->data['render_coupon_qrcode']->coupon, $this->data['render_coupon_qrcode']->coupon_qr, $this->data['img_qrcode'], $this->data['baseurl']);
            if($run == 200)
            {
                $data_session = array(
                    'coupon'        => null,
                    'coupon_qr'     => null,
                    'img_qrcode'    => null
                );
                $this->session->set_userdata($data_session);
                
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/ecoupon/index";</script>');
            }
        }else{
			$this->data['render_coupon_qrcode'] = $this->Mecoupon->renderEcouponOne();
            $this->data['img_qrcode'] = $this->Mecoupon->renderQrCode($this->data['render_coupon_qrcode']->coupon_qr, 'qrcode/');
        
            $data_session = array(
                'coupon'        => $this->data['render_coupon_qrcode']->coupon,
                'coupon_qr'     => $this->data['render_coupon_qrcode']->coupon_qr,
                'img_qrcode'    => $this->data['img_qrcode']
            );
            $this->session->set_userdata($data_session);
		}
        
        $this->data['month'] = $this->select_month();
        $this->data['year'] = $this->select_year();
        $this->data['arrRestaurant'] = $this->Mecoupon->dbgetrestaurant();
        
        $this->data['action'] = 'addecoupon';
        $this->load->view('template/layout', $this->data);
    }
    
    public function editecoupon()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/ecoupon/index');
            exit();
        }
        $this->data['id'] = $this->getParamUri(4,1);
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', 'Tên Ecoupon', 'required');
        $this->form_validation->set_rules('sale_off', 'Số tiền giảm giá', 'required');
        $this->form_validation->set_rules('amount', 'Số lượng', 'required|less_than[1000]|greater_than[0]');
        $this->form_validation->set_rules('date_user', 'Thời hạn sử dụng', 'required');
        $this->form_validation->set_rules('brief', 'Giới thiệu Ecoupon', 'required');
        $this->form_validation->set_rules('subject_email', 'Subject Email', 'required');
        $this->form_validation->set_rules('note_email', 'Lưu ý Email', 'required');
        
        $this->data['data'] = $this->Mecoupon->getEcounponData($this->data['id']);
        
        /*$data['type'] = $this->getParamInt('incentives_type');
        if($data['type'] == 1){
            $this->form_validation->set_rules('sl_month', 'Tháng sử dụng counpon', 'required');
            $this->form_validation->set_rules('sl_year', 'Năm sử dụng counpon', 'required');
        }*/
        $data['type_sl'] = $this->getParamInt('restaurant_target');
        if($data['type_sl'] == 1){
            $this->form_validation->set_rules('restaurants', 'Nhà hàng', 'required');
        }
        
       
        if($this->form_validation->run() === TRUE)
        {
            /*if($data['type'] == 1){
                $data['month'] = $this->getParamInt('sl_month');
                $data['year'] = $this->getParamInt('sl_year');
            }else{
                $data['month'] = $data['year']  = 0;
            }*/
            $data['month'] = $data['year']  = 0;
            
            if($data['type_sl'] == 1){
                $arr_restaurant = $this->getParamArray('restaurants');
            }else{
                $arr_restaurant = 0;
            }
            
			//ảnh đại diện
			/*$dataimage = $this->getParamString('img_incens_hd_');
            $dataimage_check = $this->getParamString('img_check');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'incentives-'.$timenow.'.jpg';
                $path = 'images/incentives/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$dataimage);
                $this->savefile($exp,$paths,$nameimagess);
                $img_thumbnail = $path.$nameimagess;
				@unlink(PROJECT_PATH.'/data/'.$dataimage_check);
            }else{
                $img_thumbnail = $dataimage_check;
            }*/
            
            $dataimage = $this->getParamString('img_incens_hd_');//die($dataimage);
            
            $dataimage_check = $this->getParamString('img_check');
            $verify_image = $this->getParamString('img_incens_verify');
            if(!in_array($verify_image, array(null, '')))
            {
                if($verify_image != $dataimage_check)
                {
                    @unlink(PROJECT_PATH.'/data/'.$verify_image);
                }
            }
            
            $path1 = 'images/ecoupon/';
			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
			$config['upload_path'] = '../data/'.$path1;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2000';
			$config['max_width']  = '10240';
			$config['max_height']  = '7680';
			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
			$this->load->library('upload', $config);
			if( !$this->upload->do_upload('fileupload')){
				$img = $dataimage_check;
			}else{
				$data_img = $this->upload->data();
				$img = $path1.$data_img['file_name'];
			}
			
            $run = $this->Mecoupon->editEcouponDB($data, $arr_restaurant, $this->session->userdata('admin_id'), $this->data['baseurl'], $this->data['id'],$img);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/ecoupon/index";</script>');
            }
        }
        
        $this->data['month'] = $this->select_month();
        $this->data['year'] = $this->select_year();
        $this->data['arrRestaurant'] = $this->Mecoupon->dbgetrestaurant();
        
        $this->data['action'] = 'editecoupon';
        $this->load->view('template/layout', $this->data);
    }
    
    private function select_month()
    {
        $data = array();
        for($start = 1;$start <= 12;$start++)
        {
            $data[$start] = 'Tháng '.$start;   
        }
        return $data;
    }
    private function select_year()
    {
        $start = 2013;
        $data = array();
        for($start;$start <= 2100;$start++)
        {
            $data[$start] = 'Năm '.$start;   
        }
        return $data;
    }
    
    public function getecoupon()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/ecoupon/index');
            exit();
        }
        $code_post = $this->getParamString('code');
        $this->data['render_coupon_qrcode'] = $this->Mecoupon->checkCode($code_post);
        $this->data['img_qrcode'] = $this->Mecoupon->renderQrCode($this->data['render_coupon_qrcode']->coupon_qr, 'qrcode/tmp/');
    }
    public function deleteecoupon()
    {
        if($this->_per->del != 4)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/ecoupon/index');
            exit();
        }
        $idArray = $this->getParamUri(4,1);
        $run = $this->Mecoupon->deleteEcouponDB($idArray);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/ecoupon/index";</script>');
        }
    }
    
    public function report()
    {
        //$this->data['data'] = $this->Mecoupon->getReportList();
        
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $this->data['per_page'] = $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mecoupon->getReportList($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        
        $this->data['action'] = 'report';
        $this->load->view('template/layout', $this->data);
    }
    
    public function reportdetail()
    {
        //$this->data['data'] = $this->Mecoupon->getReportThis($this->getParamUri(4,1));
        
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['ids'] = $this->getParamUri(4,1);
        $this->data['id_active'] = $this->getParamUri(5,3);
        $this->load->library('pagination');
        $this->data['per_page'] = $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(5,3);
        $offset = (($this->getParamUri(5,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(5,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mecoupon->getReportThis($config['per_page'],$offset,$this->getParamUri(4,1));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3).'/'.$this->uri->segment(4).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 5;
        $config['uri_segment_page'] = $this->getParamUri(5,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        $this->data['info'] = $this->Mecoupon->getEcouponInfo($this->getParamUri(4,1));
        
        $this->data['action'] = 'reportdetail';
        $this->load->view('template/layout', $this->data);
    }
    
    public function ajaxreporttable()
    {
        $this->data['info'] = $this->Mecoupon->getEcouponInfo($this->getParamUri(4,1));
        $this->data['data'] = $this->Mecoupon->getReportExport($this->getParamUri(4,1));
        $this->data['action'] = 'ajaxreporttable';
        $this->load->view('template/ecoupon/ajaxreporttable', $this->data);
    }
}